<template>
  <div>
    <button @click="kaishi()">开始</button>
    <button @click="ting()">停止</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      timer: null,
      num:0,
    };
  },

  components: {},

  methods: {
    kaishi() {
      if (this.timer == null) {
        this.timer = setInterval(() => {
            this.num++;
          console.log("开始定时...每过一秒执行一次==="+this.num);
          if(this.num==15){
            clearInterval(this.timer);
            console.log(this.timer);
          }
        }, 1000);
      }
    },

    ting(){
         if (this.timer != null) {
            clearInterval(this.timer);
            this.timer = null;
            console.log(this.timer);
         }
    }

  },
};
</script>

<style scoped>
</style>